local joy__shenwei = fk.CreateSkill {

  name = "joy__shenwei",

  tags = {  },

}



joy__shenwei:addEffect(fk.TurnStart, {
  name = "joy__shenwei",
  anim_type = "support",
  events = {fk.TurnStart, fk.DamageInflicted},
  can_trigger = function(self, event, target, player, data)
    if event == fk.TurnStart then
      return target == player and player:hasSkill(joy__shenwei.name) and
      table.find(player.room.alive_players, function (p) return p:getMark("@@joy__secure") == 0 end)
    else
      return not player.dead and target:getMark("@@joy__secure") == player.id
    end
  end,
  on_cost = function (self, event, target, player, data)
    if event == fk.TurnStart then
      local targets = table.filter(player.room.alive_players, function (p) return p:getMark("@@joy__secure") == 0 end)
      if #targets == 0 then return false end
      local tos = player.room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1, "#joy__shenwei-choose", joy__shenwei.name, true)
      if #tos > 0 then
        event:setCostData(self, tos[1])
        return true
      end
    else
      return player.room:askForSkillInvoke(target, joy__shenwei.name, nil, "#joy__shenwei-invoke:"..player.id)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.TurnStart then
      room:setPlayerMark(room:getPlayerById(event:getCostData(self)), "@@joy__secure", player.id)
    else
      room:setPlayerMark(target, "@@joy__secure", 0)
      room.logic:trigger("fk.MarkChanged", target, { name = "@@joy__secure", num = -1 })
      room:damage{
        from = data.from,
        to = player,
        damage = data.damage,
        damageType = data.damageType,
        skillName = data.skillName,
        chain = data.chain,
        card = data.card,
      }
      return true
    end
  end,
})
joy__shenwei:addEffect(fk.DamageInflicted, {
  name = "joy__shenwei",
  anim_type = "support",
  events = {fk.TurnStart, fk.DamageInflicted},
  can_trigger = function(self, event, target, player, data)
    if event == fk.TurnStart then
      return target == player and player:hasSkill(joy__shenwei.name) and
      table.find(player.room.alive_players, function (p) return p:getMark("@@joy__secure") == 0 end)
    else
      return not player.dead and target:getMark("@@joy__secure") == player.id
    end
  end,
  on_cost = function (self, event, target, player, data)
    if event == fk.TurnStart then
      local targets = table.filter(player.room.alive_players, function (p) return p:getMark("@@joy__secure") == 0 end)
      if #targets == 0 then return false end
      local tos = player.room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1, "#joy__shenwei-choose", joy__shenwei.name, true)
      if #tos > 0 then
        event:setCostData(self, tos[1])
        return true
      end
    else
      return player.room:askForSkillInvoke(target, joy__shenwei.name, nil, "#joy__shenwei-invoke:"..player.id)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.TurnStart then
      room:setPlayerMark(room:getPlayerById(event:getCostData(self)), "@@joy__secure", player.id)
    else
      room:setPlayerMark(target, "@@joy__secure", 0)
      room.logic:trigger("fk.MarkChanged", target, { name = "@@joy__secure", num = -1 })
      room:damage{
        from = data.from,
        to = player,
        damage = data.damage,
        damageType = data.damageType,
        skillName = data.skillName,
        chain = data.chain,
        card = data.card,
      }
      return true
    end
  end,
})
joy__shenwei:addEffect(fk.Deathed, {
  refresh_events = {fk.Deathed, fk.EventLoseSkill},
  can_refresh = function (self, event, target, player, data)
    if event == fk.Deathed then
      return target == player and player:hasSkill(self, true, true)
    else
      return target == player and data == self
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    for _, p in ipairs(room.alive_players) do
      if p:getMark("@@joy__secure") == player.id then
        room:setPlayerMark(p, "@@joy__secure", 0)
      end
    end
  end,
})
joy__shenwei:addEffect(fk.EventLoseSkill, {
  refresh_events = {fk.Deathed, fk.EventLoseSkill},
  can_refresh = function (self, event, target, player, data)
    if event == fk.Deathed then
      return target == player and player:hasSkill(self, true, true)
    else
      return target == player and data == self
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    for _, p in ipairs(room.alive_players) do
      if p:getMark("@@joy__secure") == player.id then
        room:setPlayerMark(p, "@@joy__secure", 0)
      end
    end
  end,
})

return joy__shenwei